package com.auxgroup.hr.front.service.modperson.impl;

import java.util.List;

import com.auxgroup.hr.front.service.modperson.ModEmpPhotoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import com.auxgroup.hr.front.commons.Constant;
import com.auxgroup.hr.front.commons.config.BusinessConfig;
import com.auxgroup.hr.front.commons.utils.File2Byte;
import com.auxgroup.hr.front.dao.domain.PsCModEmpPhoto;
import com.auxgroup.hr.front.dao.repository.PsCModEmpPhotoRepository;

/** 
 * @author lvliuhao
 * @version 创建时间：180526 
 * 人员照片流程信息service
 */
@Service("modEmpPhotoService")
public class ModEmpPhotoServiceImpl implements ModEmpPhotoService {
	private static final Logger logger = LoggerFactory.getLogger(ModEmpPhotoServiceImpl.class);
	
	@Autowired
	private PsCModEmpPhotoRepository PsCModEmpPhotoRepository;
	
	@Autowired
	private BusinessConfig businessConfig;


	@Override
	public PsCModEmpPhoto getModEmpPhoto(String transNum) {
		return PsCModEmpPhotoRepository.findOne(transNum);
	}


	@Override
	public PsCModEmpPhoto save(PsCModEmpPhoto ModEmpPhoto) {
		return PsCModEmpPhotoRepository.save(ModEmpPhoto);
	}


	@Override
	public Boolean delete(String transNum) {
		try {
			PsCModEmpPhotoRepository.delete(transNum);
		} catch (Exception e) {
			logger.error("delete ModEmpPhoto error: modPhotoTactPerPrimary={}",transNum,e);
			return false;
		}
		return true;
	}
	
	
	@Override
	public List<PsCModEmpPhoto> getAll(String emplid) {
		PsCModEmpPhoto ModEmpPhoto = new PsCModEmpPhoto();
		ModEmpPhoto.setEmplid(emplid);
		return PsCModEmpPhotoRepository.findAll(Example.of(ModEmpPhoto));
	}
	
	@Override
	public String buildEmplPhoto(PsCModEmpPhoto EmplPhoto) {
		StringBuilder jpgPath = new StringBuilder(businessConfig.getEmplePhotoResourceLocation());
		StringBuilder jpgName = new StringBuilder(EmplPhoto.getcTransactionNbr());
		jpgName.append(EmplPhoto.getEmplid());
		jpgName.append(".jpg");
		jpgPath.append(jpgName);
		Boolean isSucc = File2Byte.byte2image(EmplPhoto.getEmployeePhoto(), jpgPath.toString(),false);
		if(isSucc){
			return Constant.EMPLPHOTO_STATIC_PATH+jpgName.toString();
		}
		return null;
	}
	
	
}
